let arr = ['foo', 'bar', 'hello', 'word', '123']
// 排列
function solution(arr) {
    let s = arr.join('')
    let res = []
    const travel = (i, str) => {
        if (str.length === s.length) {
            res.push(str)
            return
        }
        for (let j = 0; j < arr.length; j++) {
            if (!str.includes(arr[j])) {
                travel(i + 1, str + arr[j])
            }
        }
    }
    travel(0, '')
    return res
}
console.log(
    solution(arr)
);

// promise.all
Function.prototype.myall = function (arr, limit) {
    return new Promise((resolve, reject) => {
        let count = 0
        let resArr = []
        arr.forEach(item => {
            if (!(item instanceof Promise)) {
                Promise.resolve(item).then(res => {
                    resArr.push(res)
                    count++
                    if (arr.length === count) {
                        resolve(resArr)
                    }
                })
            }
            else {
                item.then(res => {
                    resArr.push(res)
                    count++
                    if (arr.length === count) {
                        resolve(resArr)
                    }
                })
            }

        })
    })
}

const p1 = new Promise((resolve) => {
    resolve(1)
})

const p2 = new Promise((resolve) => {
    resolve(2)
})
const p3 = new Promise((resolve) => {
    resolve(3)
})

let arrT = [p1, p2, p3]

Promise.myall(arrT).then(res => {
    console.log(res)
})

// 控制并发请求数量
async function Scheduler(task, limit) {
    const res = []

    for (let i = 0; i < task.length; i += limit) {
        // 切片 Promise.all的参数
        const taskArr = task.slice(i, i + limit)
        const result = await Promise.all(taskArr)
        console.log(1);
        res.push(...result)
    }
    // console.log(res, 1111);
    return res
}

Scheduler(arrT, 3).then(res => {
    console.log(res);
})
// console.log(res, 123);